﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using VelinoStudio.Updater;
using VelinoStudio.Updater.Download;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Downloader downloader = new Downloader();
            long downloaded = 0;
            long downloadedp = 0;
            downloader.Downloading += (s, ed) =>
              {
                  richTextBox1.AppendText($"文件：{ed.DownloadingFile.DownloadFile}，大小：{calc(ed.DownloadingFile.ContentLength)}，已下载：{calc(ed.DownloadingFile.DownloadedSize)}，{DateTime.Now.ToString("HH:mm:ss fff")}{Environment.NewLine}");
                  downloaded = ed.DownloadingFile.DownloadedSize;
              };
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000;
            timer.Tick += (s, ed) =>
              {

                  labelControl1.Text = calc(downloaded - downloadedp);
                  
                  downloadedp = downloaded;

              };
            timer.Start();
            downloader.HttpDownload(@"http://velinostudio-1251454612.cos.ap-nanjing.myqcloud.com/soft/ERPBrowser/ChromiumUpdate/ChromiumUpdate_v88.2.90.0.Bin",@"d:\1.bin");
        }

        public string calc(long size)
        {
            if (size < 1024)
            {
                return $"{size} Byte";
            }
            else if (size < 1024 * 1024)
            {
                return $"{size / 1024} Kb";
            }
            else if (size < 1024 * 1024 * 1024)
            {
                return $"{size / 1024 / 1024} Mb";
            }
            else
            {
                return size.ToString();
            }
        }
    }
}
